package club.pinea.handler.login;

import club.pinea.annotations.Head;
import club.pinea.interfaces.TCPMessage;
import club.pinea.proto.BasicProto;
import club.pinea.proto.CommandProto;
import club.pinea.proto.UserProto;
import club.pinea.send.MessageSend;
import club.pinea.service.UserService;
import club.pinea.session.IOSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description: 修改密码handler处理类
 * @author: 华上进
 * @createTime: 2021/6/26 14:43
 */
@Head(CommandProto.Command.C_S_USER_RESET_PASSWORD_VALUE)
@Component
public class UpdateResetPasswordHandler implements TCPMessage<BasicProto.BaseMessage> {
    // 用户接口
    @Autowired
    private UserService loginUserService;

    @Override
    public void doMessage(BasicProto.BaseMessage msg, IOSession session) throws Exception {
        UserProto.UpdatePwdRequest updatePwdRequest = UserProto.UpdatePwdRequest.parseFrom(msg.getBody());
        UserProto.UpdatePwdResponse updatePwdResponse = null;
        String name = updatePwdRequest.getName();
        String oldPass = updatePwdRequest.getOldPass();
        String newPass = updatePwdRequest.getNewPass();
        int result = loginUserService.resetPassword(name, oldPass, newPass);
        if (result > 0) {
            updatePwdResponse = UserProto.UpdatePwdResponse.newBuilder().
                    setRes(MessageSend.successRes("密码修改成功！")).build();
            MessageSend.sendToUser(updatePwdResponse, session);
        } else {
            updatePwdResponse = UserProto.UpdatePwdResponse.newBuilder().
                    setRes(MessageSend.errorRes("请确保旧密码密码正确！")).build();
            MessageSend.sendToUser(updatePwdResponse, session);
        }
    }
}
